package com.smile.frame.alibaba.services.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 自定义全局过滤器
 *
 * @author LGC
 * @date 2021/12/9 11:24
 */
@Slf4j
@Component
public class CustomerGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        HttpHeaders headers = exchange.getRequest().getHeaders();
        log.info("custom global filter1");
        // 登陆验证
//        String token = headers.getFirst("token");
//        if (StrUtil.isBlank(token)) {
//            ServerHttpResponse response = exchange.getResponse();
//            ResponseVO<Object> responseVO = ResponseVO.error(BaseErrorCodeEnum.BASE_LOGIN_ERROR);
//            byte[] bytes = JSONUtil.toJsonStr(responseVO).getBytes(StandardCharsets.UTF_8);
//            DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
//            response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
//            return response.writeWith(Flux.just(buffer));
//        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
